package com.example.mywanandroid.ui.bottom.wechat;

import android.os.Bundle;

import androidx.lifecycle.ViewModelProvider;

import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.bean.responsebean.WeChatBean;
import com.example.mywanandroid.config.Constants;
import com.example.mywanandroid.databinding.FragmentViewPagerBinding;
import com.example.mywanandroid.navinterface.ScrollToTop;
import com.example.mywanandroid.ui.adapter.ArticleListPagerAdapter;
import com.example.mywanandroid.ui.articlelist.ArticleListFragment;

import java.util.List;

public class WeChatFragment extends BaseFragment<FragmentViewPagerBinding, WeChatViewModel> implements ScrollToTop {
    private ArticleListPagerAdapter mPagerAdapter;
    private List<WeChatBean> mList;
    private int pos;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_view_pager;
    }

    @Override
    protected void handleArguments(Bundle args) {
        super.handleArguments(args);
        pos = args.getInt(Constants.ParamCode.PARAM2, 0);
        mList = (List<WeChatBean>) args.getSerializable(Constants.ParamCode.PARAM1);
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(WeChatViewModel.class);
    }

    @Override
    protected void bindViewModel() {
        mDataBinding.setViewModel(mViewModel);
    }

    @Override
    protected boolean isSupportLoad() {
        return true;
    }

    @Override
    protected void init() {
        if (mList != null) {

        } else {
            mViewModel.loadWeChatList();
        }

        mPagerAdapter = new ArticleListPagerAdapter(getChildFragmentManager());
        mDataBinding.viewPager.setAdapter(mPagerAdapter);
        mDataBinding.tabLayout.setupWithViewPager(mDataBinding.viewPager);

        mDataBinding.viewPager.setCurrentItem(pos);
    }

    @Override
    public void scrollToTop() {
        ArticleListFragment fragment = (ArticleListFragment) mPagerAdapter.getItem(mDataBinding.viewPager.getCurrentItem());
        fragment.scrollToTop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPagerAdapter != null) {
            mPagerAdapter.release();
        }
    }
}
